package data;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class RoomType extends AbstractData {
	private long id = -1;
	private transient long hotelId;
	private String title;
	private String shortName;
	private String pic;
	private String description;
	private String imageCaption;
	private long StandardOccupancy;
	private long MaxOccupancy;
	private long extraBedAllowed;
	private List<Amenity> amenityList;
	private String amenityListString;
	private double basePrice;
	private double extraBedPrice;
	private List<Tax> applicableTaxList;
	private String applicableTaxListString;
	private boolean status;
	private boolean avaliable;
	private long sortOrder;
	private double upchargeperson;

	public RoomType() {
		status = true;
		avaliable = true;
	}

	public RoomType(long id) {
		this.id = id;
		this.status = true;
		this.avaliable = true;
	}
	@Override
	public long getId() {
		return id;
	}
	
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public long getHotelId() {
		return hotelId;
	}
	public void setHotelId(long hotelId) {
		this.hotelId = hotelId;
	}

	public String getShortName() {
		return shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
	}
	public String getImageCaption() {
		return imageCaption;
	}

	public void setImageCaption(String imageCaption) {
		this.imageCaption= imageCaption;
	}
	public long getStandardOccupancy() {
		return StandardOccupancy;
	}
	public void setStandardOccupancy(long standardOccupancy) {
		StandardOccupancy = standardOccupancy;
	}
	public long getMaxOccupancy() {
		return MaxOccupancy;
	}
	public void setHigherOccupancy(long MaxOccupancy) {
		this.MaxOccupancy = MaxOccupancy;
	}
	public long getExtraBedAllowed() {
		return extraBedAllowed;
	}
	public void setExtraBedAllowed(long extraBedAllowed) {
		this.extraBedAllowed = extraBedAllowed;
	}
	public List<Amenity> getAmenityList() {
		if ((amenityList == null || amenityList.size() == 0) && !(amenityListString == null || amenityListString.isEmpty())) {
			String[] idOfList = SplitList(amenityListString);
			List<Amenity> list = new ArrayList<Amenity>();
			for (String id : idOfList) {
				Amenity a = new Amenity(Long.parseLong(id));
				list.add(a);
			}
			
			for(Amenity a : list)
				a.LoadData();
			
			amenityList = list;
		}
		return amenityList;
	}
	
	
	public void setAmenityListString(String amenityListString) {
		this.amenityListString = amenityListString;
	}

	public void setApplicableTaxListString(String applicableTaxListString) {
		this.applicableTaxListString = applicableTaxListString;
	}

	public void setAmenityList(List<Amenity> amenityList) {
		this.amenityListString = CombineList(amenityList);
		this.amenityList = amenityList;
	}
	public double getBasePrice() {
		return basePrice;
	}
	public void setBasePrice(double basePrice) {
		this.basePrice = basePrice;
	}
	public double getExtraBedPrice() {
		return extraBedPrice;
	}
	public void setExtraBedPrice(double extraBedPrice) {
		this.extraBedPrice = extraBedPrice;
	}
	public List<Tax> getApplicableTaxList() {
		if ((applicableTaxList == null || applicableTaxList.size() == 0) && !(applicableTaxListString == null || applicableTaxListString.isEmpty())) {
			String[] idOfList = SplitList(applicableTaxListString);
			List<Tax> list = new ArrayList<Tax>();
			for (String id : idOfList) {
				Tax t = new Tax(Long.parseLong(id));
				list.add(t);
			}
			
			for(Tax t : list)
				t.LoadData();
			
			applicableTaxList = list;
		}
		return applicableTaxList;
	}
	public void setApplicableTaxList(List<Tax> applicableTaxList) {
		this.applicableTaxListString = CombineList(applicableTaxList);
		this.applicableTaxList = applicableTaxList;
	}
	public boolean isStatus() {
		return status;
	}
	public void setStatus(boolean status) {
		this.status = status;
	}
	public boolean isAvaliable() {
		return avaliable;
	}
	public void setAvaliable(boolean avaliable) {
		this.avaliable = avaliable;
	}

	public long getSortOrder() {
		return sortOrder;
	}
	public void setSortOrder(long sortOrder) {
		this.sortOrder = sortOrder;
	}
	
	public String getPic() {
		return pic;
	}

	public void setPic(String pic) {
		this.pic = pic;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getAmenityListString() {
		return amenityListString;
	}

	public String getApplicableTaxListString() {
		return applicableTaxListString;
	}

	public void setId(long id) {
		this.id = id;
	}

	public void setMaxOccupancy(long maxOccupancy) {
		MaxOccupancy = maxOccupancy;
	}

	public double getUpchargeperson() {
		return upchargeperson;
	}

	public void setUpchargeperson(double upchargeperson) {
		this.upchargeperson = upchargeperson;
	}

	@Override
	public Entity getEntity() {

		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.RoomType);
		else
			item = new Entity(this.key);

		item.setProperty("hotelId", this.hotelId);
		item.setProperty("title", this.title);
		item.setProperty("shortName", this.shortName);
		item.setProperty("imageCaption", this.imageCaption);
		item.setProperty("description", this.description);
		item.setProperty("pic", this.pic);
		item.setProperty("StandardOccupancy", this.StandardOccupancy);
		item.setProperty("MaxOccupancy", this.MaxOccupancy);
		item.setProperty("extraBedAllowed", this.extraBedAllowed);
		item.setProperty("amenityList", this.amenityListString);
		item.setProperty("basePrice", this.basePrice);
		item.setProperty("extraBedPrice", this.extraBedPrice);
		item.setProperty("applicableTaxList", this.applicableTaxListString);
		item.setProperty("status", this.status);
		item.setProperty("avaliable", this.avaliable);
		item.setProperty("sortOrder", this.sortOrder);
		item.setProperty("upchargeperson", this.upchargeperson);
		return item;
	}
	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
		this.hotelId = (Long) entity.getProperty("hotelId");
		this.title = (String) entity.getProperty("title");
		this.shortName = (String) entity.getProperty("shortName");
		this.imageCaption = (String) entity.getProperty("imageCaption");
		this.StandardOccupancy = (Long) entity.getProperty("StandardOccupancy");
		this.MaxOccupancy = (Long) entity.getProperty("MaxOccupancy");
		this.extraBedAllowed = (Long) entity.getProperty("extraBedAllowed");
		this.amenityListString = (String) entity.getProperty("amenityList");
		this.basePrice = (Double) entity.getProperty("basePrice");
		this.extraBedPrice = (Double) entity.getProperty("extraBedPrice");
		this.applicableTaxListString = (String) entity.getProperty("applicableTaxList");
		this.status = (Boolean) entity.getProperty("status");
		this.avaliable = (Boolean) entity.getProperty("avaliable");
		this.sortOrder = (Long) entity.getProperty("sortOrder");
		this.description = (String) entity.getProperty("description");
		this.pic = (String) entity.getProperty("pic");
		//TODO: remove if 
		if(entity.getProperty("upchargeperson") != null){
			this.upchargeperson = (Double) entity.getProperty("upchargeperson");
		}
		this.key = entity.getKey();
	}

	public void LoadData() {
		if (this.id > -1) {
			Entity entity = LoadData(EntityName.RoomType, this.id);
			setEntity(entity);
		}
	}
}
